-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.py
More file actions
30 lines (24 loc) · 747 Bytes
/
Solution.py
File metadata and controls
30 lines (24 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def is_bst_util(root, min_val, max_val):
if root is None:
return True
if root.data <= min_val or root.data >= max_val:
return False
return (is_bst_util(root.left, min_val, root.data) and
is_bst_util(root.right, root.data, max_val))
def is_bst(root):
return is_bst_util(root, float('-inf'), float('inf'))
if __name__ == "__main__":
root = Node(10)
root.left = Node(5)
root.right = Node(20)
root.left.left = Node(3)
root.left.right = Node(7)
if is_bst(root):
print("The tree is a valid BST.")
else:
print("The tree is not a valid BST.")